import * as types from './mutation-types'
import * as constant from '../common/constant'
import { transform } from '../common/uri'
import Vue from 'vue'
import axios from 'axios'
import * as utils from '../common/utils'


function loadTask(state) {
  var entryUri = "/backend/async/list";
  var uri = '/api/consume/udf?uri=' + entryUri + '&f=running';
  axios.post(constant.API_SERVER + uri).then(response => {
      state.taskCount = response.data.total;
      state.tasks = response.data.data;
      if (state.taskCount > 5) {
        state.tasks = state.tasks.slice(0, 5);
      }
    }, response => {
    }
  );
}

function loadEnvelopes(state) {
  var entryUri = "/backend/envelope/unread";
  var uri = '/api/consume/fetch?uri=' + entryUri;
  axios.post(constant.API_SERVER + uri).then(response => {
      state.envelopeCount = response.data.total;
      state.envelopes = response.data.data;
      if (state.envelopeCount > 5) {
        state.envelopes = state.envelopes.slice(0, 5);
      }
    }, response => {
    }
  );
}

function savePageTitleMap(nav) {
  var titleMap = {}
  for (var i = 0; i < nav.length; i++) {
    var first = nav[i].first;
    for (var j = 0; j < first.length; j++) {
      var second = first[j].second;
      for (var k = 0; k < second.length; k++) {
        titleMap[second[k].uri] = second[k].caption;
      }
    }
  }
  localStorage.titleMap = JSON.stringify(titleMap);
}


export default {
  [types.LOAD_NAVBARS] (state, uri) {
    if (localStorage.nav && ! constant.DEV_MODE) {
      state.data = JSON.parse(localStorage.nav);
      if (state.data.length > 0) {
        return;
      }
    }
    axios.post(constant.API_SERVER + "/api/nav/fetch").then(response => {
        state.data = response.data;
        localStorage.nav = JSON.stringify(state.data);
        savePageTitleMap(response.data)
      }, error => {
        utils.auth(error);
      }
    );
  },
  [types.LOAD_MOBILE_NAVBARS](state) {
    if (localStorage.mobileNav && !constant.DEV_MODE) {
      state.mobileData = JSON.parse(localStorage.mobileNav);
      if (state.mobileData) {
        return;
      }
    }
    axios.post(constant.API_SERVER + "/api/nav/fetch?type=mobile").then(response => {
      state.mobileData = response.data;
      localStorage.mobileNav = JSON.stringify(state.mobileData);
    }, error => {
      // error callback
      console.log(error);
    }
    );
  },
  [types.TIMER_TASKS] (state) {
    loadTask(state);
    var defaultTime = 1000 * 15;
    let interval = window.setInterval(function() {
      if (state.taskCount <= 0) {
        window.clearInterval(interval);
      } else {
        loadTask(state);
      }
    }, defaultTime);
  },
  [types.LOAD_ENVELOPES] (state) {
    //loadEnvelopes(state);
  },
  [types.MARK_READ] (state, link) {
    var uri = '/api/consume/load?uri=' + link.mainUri + '&f=markRead' + "&id=" + link.id; + "&q=''"
    axios.post(constant.API_SERVER + uri).then(response => {
        if (response.data.type != 'danger') {
          //loadEnvelopes(state);
        }
      }, response => {
      }
    );
  },
  [types.UPDATE_TOP_INDEX] (state, id) {
    state.topIndex = id;
  },
  [types.SAVE_USER_LOCALE] (state, locale) {
    axios.post(constant.API_SERVER + "/saveUserLocale?locale=" + locale, locale).then(response => {
      }, response => {
      }
    );
  },
};
